package com.example.demo;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private List<App> apps;

    public void setData(List<App> apps){
        this.apps = apps;
        notifyDataSetChanged();

    }


    //指定item的布局
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_app,parent,false);

        return new MyViewHolder(view);
    }

    //绑定数据
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        App app = apps.get(position);
        holder.tvMyName.setText(app.getName());


    }

    //返回item数量
    @Override
    public int getItemCount() {

        return apps == null ? 0 : apps.size();
    }

    //复用布局
    static class  MyViewHolder extends RecyclerView.ViewHolder {

        TextView tvMyName;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvMyName = itemView.findViewById(R.id.tv_app_name);
        }
    }

}
